//
//  SecondWebViewController.m
//  INAlienLand
//
//  Created by wYz on 15/7/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SecondWebViewController.h"
#import "MBProgressHUD.h"
@interface SecondWebViewController ()
//收藏判断
@property(nonatomic,assign)BOOL isYesorNo;
//网址
@property(nonatomic,retain)NSMutableArray *UrlArr;
//收藏成功提示label
@property(nonatomic,retain)UILabel *addLabel;
//取消收藏提示label
@property(nonatomic,retain)UILabel *cancelLabel;



@end

@implementation SecondWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    UIWebView* webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49-64)];
    
    [self.view addSubview:webView];
    NSString *str = self.Url;
    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
    
    
    [webView setBackgroundColor:[UIColor colorWithRed:0.987 green:1.000 blue:0.806 alpha:1.000]];
    [webView loadRequest:request];
    
    [webView goBack];
    [webView goForward];
    [webView reload];
    [self UnArch];
    [self button];
   
   //   NSLog(@"%@",str);
    
    //添加一个清扫的手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGest:)];
    [webView addGestureRecognizer:swipe];
    //向右
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    


    
}

- (void)button{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 30, 30);
    button.backgroundColor = [UIColor redColor];
    button.backgroundColor = [UIColor clearColor];
    // 根据状态换图片
    if (self.isYesorNo) {
        [button setImage:[UIImage imageNamed:@"iconfont-xiai-2.png"] forState:UIControlStateNormal];
    } else {
        [button setImage:[UIImage imageNamed:@"iconfont-xiai-3.png"] forState:UIControlStateNormal];
    }
    
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 1000;
    
    //
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    
    
}

- (void)click:(UIButton *)button{
    
    if (self.isYesorNo) {
        NSInteger index = 0;
        for (NSInteger i = 0; self.UrlArr.count; i++) {
            if ([self.UrlArr[i] isEqualToString:self.Url]) {
                index = i;
                break;
            }
            
        }
        [self.UrlArr removeObjectAtIndex:index];
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
        NSString *sandBoxPath = sandBox[0];
        NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Url.plist"];
        [NSKeyedArchiver archiveRootObject:self.UrlArr toFile:documentPath];
        
        
        [button setImage:[UIImage imageNamed:@"iconfont-xiai-3.png"] forState:UIControlStateNormal];
        self.cancelLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width*4/10, self.view.frame.size.height*3/4, self.view.frame.size.width/5, 15)];
        self.cancelLabel.text = @"已取消收藏";
        [self.view addSubview:self.cancelLabel];
        self.cancelLabel.backgroundColor = [UIColor blackColor];
        self.cancelLabel.textAlignment = NSTextAlignmentCenter;
        self.cancelLabel.font = [UIFont systemFontOfSize:12];
        self.cancelLabel.textColor = [UIColor whiteColor];
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(cancel) userInfo:nil repeats:NO];
        NSLog(@"取消成功");
        self.isYesorNo = NO;
    }else{
        
        [self.UrlArr addObject:self.Url];
        
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
        NSString *sandBoxPath = sandBox[0];
        NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Url.plist"];
        [NSKeyedArchiver archiveRootObject:self.UrlArr toFile:documentPath];
        
        [button setImage:[UIImage imageNamed:@"iconfont-xiai-2.png"] forState:UIControlStateNormal];
        self.isYesorNo = YES;
        
        self.addLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width*4/10, self.view.frame.size.height*3/4, self.view.frame.size.width/5, 15)];
        self.addLabel.text = @"收藏成功";
        [self.view addSubview:self.addLabel];
        self.addLabel.backgroundColor = [UIColor blackColor];
        self.addLabel.textAlignment = NSTextAlignmentCenter;
        self.addLabel.font = [UIFont systemFontOfSize:13];
        self.addLabel.textColor = [UIColor whiteColor];
        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(add) userInfo:nil repeats:NO];
        NSLog(@"收藏成功");
        NSLog(@"%@",documentPath);
    }
    
    
    
    
}


- (void)UnArch{
    
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
        NSString *sandBoxPath = sandBox[0];
        NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Url.plist"];
        self.UrlArr = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
        self.isYesorNo = NO;
        if (self.UrlArr==nil) {
            self.UrlArr = [NSMutableArray array];
        }else{
            for (NSInteger i = 0; i < self.UrlArr.count; i++) {
                if ([self.UrlArr[i] isEqualToString:self.Url]) {
                    self.isYesorNo = YES;
                    break;
                }
            }
        }
    
}


- (void)cancel{
    
    [self.cancelLabel removeFromSuperview];
    
    
}

- (void)add{
    
    [self.addLabel removeFromSuperview];
    
}
- (void)swipeGest:(UISwipeGestureRecognizer *)swipe{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
